package oktx.feign;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by huangdachao on 2018/5/31 15:24.
 */
public class FeignContext {
    private static final ThreadLocal<FeignContext> LOCAL = ThreadLocal.withInitial(FeignContext::new);
    private final Map<String, String> attachments = new HashMap<>();
    private String url;
    private String requestMethod;
    private Object[] arguments;
    private Method method;
    private String host;
    private int port;

    private FeignContext() { }

    /**
     * get context.
     *
     * @return context
     */
    public static FeignContext getContext() {
        return LOCAL.get();
    }

    /**
     * remove context.
     *
     * @see com.alibaba.dubbo.rpc.filter.ContextFilter
     */
    public static void removeContext() {
        LOCAL.remove();
    }

    /**
     * get attachment.
     *
     * @param key
     * @return attachment
     */
    public String getAttachment(String key) {
        return attachments.get(key);
    }

    /**
     * set attachment.
     *
     * @param key
     * @param value
     * @return context
     */
    public void setAttachment(String key, String value) {
        if (value == null) {
            attachments.remove(key);
        } else {
            attachments.put(key, value);
        }
    }

    /**
     * remove attachment.
     *
     * @param key
     * @return context
     */
    public FeignContext removeAttachment(String key) {
        attachments.remove(key);
        return this;
    }

    /**
     * set attachments
     *
     * @param attachment
     * @return context
     */
    public FeignContext setAttachments(Map<String, String> attachment) {
        this.attachments.clear();
        if (attachment != null && attachment.size() > 0) {
            this.attachments.putAll(attachment);
        }
        return this;
    }

    public Map<String, String> getAttachments() {
        return attachments;
    }

    public String getUrl() {
        return url;
    }

    public FeignContext setUrl(String url) {
        this.url = url;
        return this;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public FeignContext setRequestMethod(String requestMethod) {
        this.requestMethod = requestMethod;
        return this;
    }

    public Object[] getArguments() {
        return arguments;
    }

    public FeignContext setArguments(Object[] arguments) {
        this.arguments = arguments;
        return this;
    }

    public Method getMethod() {
        return method;
    }

    public FeignContext setMethod(Method method) {
        this.method = method;
        return this;
    }

    public String getHost() {
        return host;
    }

    public FeignContext setHost(String host) {
        this.host = host;
        return this;
    }

    public int getPort() {
        return port;
    }

    public FeignContext setPort(int port) {
        this.port = port;
        return this;
    }
}
